/**
 * 
	1���ṩDIVģ̬����ʵ��function����FDCDialog
	2���ṩDIV�϶�Ч��ʵ��function����COASDrag
 * @return
 */
var FDCDialog = new FDCDialog1("default");
document.write("<link href='css/FDCDialog.css' type='text/css' rel='stylesheet'>");
function FDCDialog1(_dialogName, _parentNode) {
	/** ������� **/
	this.dialogName = _dialogName;
	/** ���ڵ� **/
	this.parentNode = _parentNode;
	/** �߿�ߴ�(����) **/
	this.cBorderSize=1;
	/** �ύ��ť�ı� **/
	this.cSubmitText="ȷ ��";
	/** ȡ��ť�ı� **/
	this.cCancelText="ȡ ��";
	this.isMSIE = true;
	this.returnValue = null;
	this.cEditStatus=false;
	this.callBackFun=function(){};
	this.cancelCallBackFun=function(){};
	this.callBackParams={};
	this.buttonFun = {};
	this.loadFinish = false;
	/** �ر�ģ̬���� **/
	this.Close=function(wait){
		this.unLockWindow();
		this.loadFinish = false;
		$("iframe", $(("#coas_dialog_" + this.dialogName))).each(function(i){
			$(this).hide();
			this.src="";
			$(this).remove();
		});
		$(("#coas_dialog_" + this.dialogName)).hide();
		$(("#coas_dialog_" + this.dialogName)).remove();
		if(this.cEditStatus && this.cancelCallBackFun)this.cancelCallBackFun();
	};
	this.scrollOffOn = function() {
		var dialog = $(("#coas_dialog_" + this.dialogName))[0];
		if (dialog.getAttribute("stopScroll") == 'true') {
			dialog.setAttribute("stopScroll", "false");
		} else {
			dialog.setAttribute("stopScroll", "true");
		}
	};
	this.lockWindow=function(isloading,path){
		if($("#lockWindow").length == 0) {
			$("body").prepend("<div id='lockWindow' align='center' style='z-index:1000;position:absolute;background-color:#111111;filter:alpha(opacity=30);moz-opacity:0.3;opacity:0.3;'>"+(isloading ? "<img style='MARGIN-TOP: 25%' src='"+path+"/images/blue-loading.gif' />" : "")+"</div>");
		}
		var width = Math.max(Math.max(document.body.scrollWidth, document.body.offsetWidth),document.body.clientWidth);
		var height = Math.max(Math.max(document.body.scrollHeight, document.body.offsetHeight),document.body.clientHeight);
		$("#lockWindow").css("height", height);
		$("#lockWindow").css("width", width);
		$("#lockWindow").show();
	};
	this.unLockWindow=function(){
		$("#lockWindow").remove();
	};
	/** �ύ **/
	this.Ok=function(){
		var frm = $("#coas_iframe_" + this.dialogName);	
		if (!frm[0] || frm[0].contentWindow.Ok()){
			//setTimeout(this.closeAndFun, 1);
			this.closeAndFun();
		} else{
			frm[0].focus() ;
		}
	};
	this.closeAndFun=function() {
		var obj = this;
		if (!obj.dialogName && FDCDialogContent.curDialogName)	
			obj = FDCDialogContent.getDialogObj("coas_dialog_" + FDCDialogContent.curDialogName);		
		var frm = $("#coas_iframe_" + obj.dialogName);
		obj.returnValue = !frm[0] ? true : frm[0].contentWindow.returnValue;
		var whileBoolean = frm[0] && frm[0].contentWindow && frm[0].contentWindow.document.forms[0] && frm[0].contentWindow.document.forms[0].getAttribute("submiting");
		while ((obj.isMSIE && whileBoolean) || (!obj.isMSIE && whileBoolean == "true")) {
			FDCDialogContent.curDialogName = obj.dialogName;
			setTimeout(obj.closeAndFun, 1);
			return;
		}
		FDCDialogContent.curDialogName = null;
		if (obj.callBackFun) {
			if (obj.callBackParams) {
				obj.callBackFun(obj.returnValue, obj.callBackParams.param1, obj.callBackParams.param2);
			} else {
				obj.callBackFun(obj.returnValue);
			}
		}
		obj.cEditStatus=false;
		obj.Close();
		
		
	};
	this.Alert=function(msg, callBackFun, callBackParams) {
		msg = msg.replace("\n", "<br>");
		msg = msg.replace("\r", "<br>");
		msg = msg.replace(" ", "&nbsp;");
		this.cSubmitText="ȷ ��";
	
		this.init("ϵͳ��ʾ", null, false, msg, false, 400, 0, null, null, true, false, null, callBackFun, null, callBackParams, true);
	};
	this.Confirm=function(msg, callBackFun, callBackParams, cancelCallBackFun) {
		this.cSubmitText="ȷ ��";
		this.cCancelText="ȡ ��";
		this.cEditStatus=true;
		this.init("ϵͳ��ʾ", null, false, msg, false, 400, 0, null, null, true, true, null, callBackFun, cancelCallBackFun, callBackParams, true);
	};
	this.SubmitOpen=function(dialogTitle, iframeSrc, iframeWidth, iframeHeight, callBackFun, callBackParams){
		this.cSubmitText="ȷ ��";
		this.cCancelText="ȡ ��";
		this.init(dialogTitle, null, true, iframeSrc, true, iframeWidth, iframeHeight, null, null, true, true, null, callBackFun, null, callBackParams, true);
	};
	this.SaveOpen=function(dialogTitle, iframeSrc, iframeWidth, iframeHeight, callBackFun, callBackParams){
		this.cSubmitText="�� ��";
		this.cCancelText="�� ��";
		this.init(dialogTitle, null, true, iframeSrc, true, iframeWidth, iframeHeight, null, null, true, true, null, callBackFun, null, callBackParams, true);
	};
	this.Open=function(dialogTitle, iframeSrc, iframeWidth, iframeHeight, isSubmitButton, isCancelButton, otherButtons, callBackFun, cancelCallBackFun, callBackParams){
		this.cSubmitText="�� ��";
		this.cCancelText="�� ��";
		this.init(dialogTitle, null, true, iframeSrc, true, iframeWidth, iframeHeight, null, null, isSubmitButton, isCancelButton, otherButtons, callBackFun, cancelCallBackFun, callBackParams);
	};
	//CreateInit
	this.init=function(dialogTitle, rightLink, isUrl, iframeSrcOrMsg, isShadow, iframeWidth, iframeHeight, top, left, isSubmitButton, isCancelButton, otherButtons, _callBackFun, _cancelCallBackFun, _callBackParams, isDrag){
		this.callBackFun = _callBackFun;
		this.callBackParams = _callBackParams;
		this.cancelCallBackFun = _cancelCallBackFun;
		//��ȡ�ͻ���ҳ����
		var _client_width = document.body.clientWidth;
		this.lockWindow();
		this.isMSIE = navigator.userAgent && navigator.userAgent.indexOf("MSIE") != -1;
		var _left = left ? left : (_client_width - (iframeWidth + this.cBorderSize * 2)) / 2;
		var offHeight = document.body.clientHeight;
		var defaultHeight = (offHeight - iframeHeight - this.cBorderSize * 2 - 30 - 30) / 4
		var _top = top ? top : document.body.scrollTop + (defaultHeight < 0 ? 20 : defaultHeight);
		/** �����������ڣ����ж��Ƿ���ڣ����������ɾ�� **/
		FDCDialogContent.removeDialogObj("coas_dialog_" + this.dialogName);
		if (typeof($("#coas_dialog_" + this.dialogName)[0]) != "undefined") $("#coas_dialog_" + this.dialogName).remove();
		this.parentNode = this.parentNode ? this.parentNode : $("body");
		var index = 1050 + FDCDialogContent.dialogObjs.length;
		this.parentNode.prepend("<div  id='coas_dialog_" + this.dialogName + "' style='z-index:" + index + ";position:absolute; display: block;'  class='aui_state_focus'></div>");
		var _coas_dialog = $("#coas_dialog_" + this.dialogName);
		FDCDialogContent.setDialogObj("coas_dialog_" + this.dialogName, this);
		_coas_dialog.css("left", (_left < 0 ? 0 : _left) + document.documentElement.scrollLeft + "px");
		_coas_dialog.css("top", (_top < 0 ? 0 : _top) + document.documentElement.scrollTop + "px");
		_coas_dialog.append("<div id='coas_dialog_o_" + this.dialogName + "' class='aui_outer'></div>");
		var _coas_dialog_o = $("#coas_dialog_o_" + this.dialogName);
		_coas_dialog_o.append("<table class='aui_border'><tbody><tr><td class='aui_nw'></td><td class='aui_n'></td><td class='aui_ne'></td></tr><tr><td class='aui_w'></td><td class='aui_c'><div class='aui_inner'><table class='aui_dialog'><tbody><tr moveDrag='true' ondblclick='FDCDialogContent.getDialogObj(\"coas_dialog_" + this.dialogName + "\").scrollOffOn();'><td class='aui_header'><div class='aui_titleBar'><div class='aui_title' style='cursor: auto; display: block;' id='coas_dialog_t_" + this.dialogName + "'></div><a href='javascript:;' " + (isCancelButton ? ("onclick='FDCDialogContent.getDialogObj(\"coas_dialog_" + this.dialogName + "\").Close();'"):"") + " class='aui_close' style='display: block;'>��</a></div></td></tr><tr><td class='aui_main' id='coas_dialog_main_td_" + this.dialogName + "' style='width: 20em; height: auto;background:none repeat scroll 0 0 #EEF3F7;'><div class='aui_content' style='padding: 20px 25px;' id='coas_dialog_c_" + this.dialogName + "'></div></td></tr><tr moveDrag='true'><td class='aui_footer'><div class='aui_buttons' id='coas_dialog_b_" + this.dialogName + "'></div></td></tr></tbody></table></div></td><td class='aui_e'></td></tr><tr><td class='aui_sw'></td><td class='aui_s'></td><td class='aui_se' style='cursor: auto;'></td></tr></tbody></table>");
		var _coas_dialog_t = $("#coas_dialog_t_" + this.dialogName);
		var _coas_dialog_c = $("#coas_dialog_c_" + this.dialogName);
		var _coas_dialog_b = $("#coas_dialog_b_" + this.dialogName);
		_coas_dialog_t.append(dialogTitle);
		if (isSubmitButton || isCancelButton || otherButtons) {
			/** ��ť�� **/
			if (isSubmitButton)
				_coas_dialog_b.append("<button class='aui_state_highlight'  onclick='FDCDialogContent.getDialogObj(\"coas_dialog_" + this.dialogName + "\").Ok();'>" + this.cSubmitText + "</button>");
			if (otherButtons) {
				for (var n = 0; n < otherButtons.names.length; n++) {
					_coas_dialog_b.append("<button class='aui_state_highlight' id='"+ (otherButtons.names[n]) +"' onclick='FDCDialogContent.getDialogObj(\"coas_dialog_" + this.dialogName + "\").cEditStatus=true;$(\"#coas_iframe_" + this.dialogName + "\")[0].contentWindow." + otherButtons.functions[n] + "(\"" + this.dialogName + "\");'>" + otherButtons.names[n] + "</button>");
				}
			}
			if (isCancelButton)
				_coas_dialog_b.append("<button class=''  onclick='FDCDialogContent.getDialogObj(\"coas_dialog_" + this.dialogName + "\").Close();'>" + this.cCancelText + "</button>");
		}
		if (isUrl) {
			_coas_dialog_c.append("<iframe id='coas_iframe_" + this.dialogName + "' dialogName='"+this.dialogName+"' style='visibility: hidden' src='"+iframeSrcOrMsg+"' style='' scrolling='auto' frameborder='0'  marginheight='0' marginwidth='0' align='middle' width='"+ (iframeWidth - 2) +"' height='"+(iframeHeight)+"' />");
		} else {
			_coas_dialog_c.append(iframeSrcOrMsg);
		}
		$("#coas_iframe_" + this.dialogName).bind("load", function(){
			this.style.visibility='visible';
			var dianame = this.getAttribute("dialogName");
			if (this.contentWindow.document.forms[0]) {
				this.contentWindow.document.forms[0].setAttribute("submiting", false);
				if (navigator.userAgent && navigator.userAgent.indexOf("MSIE") != -1){
					var submit = this.contentWindow.document.forms[0].submit;
					this.contentWindow.document.forms[0].submit = function(){
						if (!this.getAttribute("submiting")) {
							this.setAttribute("submiting", true);
							submit();
						}
					};
				}
				if(!parseInt(this.height)){
					FDCDialogContent.getDialogObj("coas_dialog_" + dianame).autoResizeHeight();
				}
			}
			FDCDialogContent.getDialogObj("coas_dialog_" + dianame).loadFinish = true;			
		});
		/** ע�ᴰ���ƶ� **/
		COASDrag(_coas_dialog[0], true);
	};
	this.runInnerFun = function(funName, funParam1, funParam2, funParam3, funParam4, funParam5, funParam6, funParam7){
		var dialogName = this.dialogName;
		if (!this.loadFinish) {setTimeout(function(){FDCDialogContent.getDialogObj("coas_dialog_" + dialogName).runInnerFun(funName, funParam1, funParam2, funParam3, funParam4, funParam5, funParam6, funParam7)}, 50); return;}
		var win = $("#coas_iframe_" + this.dialogName )[0].contentWindow;
		win.eval(funName + "(\'" + funParam1+"\', \'"+funParam2+"\', \'"+funParam3+"\', \'"+funParam4+"\', \'"+funParam5+"\', \'"+funParam6+"\', \'"+funParam7 + "\')");	
	};
	/**����Ӧ�߶�**/
	this.autoResizeHeight = function(){
		var win = $("#coas_iframe_" + this.dialogName)[0].contentWindow;
		var sH = win.document.body.scrollHeight;
		$("#coas_iframe_" + this.dialogName).css("height", sH);
	};
	/**���ð�ť**/
	this.reBuildButton = function(isSubmitButton, isCancelButton, otherButtons){
			/** ��ť�� **/
			var _coas_dialog_b = $("#coas_dialog_b_" + this.dialogName);
			_coas_dialog_b.html("");
			if (isSubmitButton)
				_coas_dialog_b.append("<button class='aui_state_highlight'  onclick='FDCDialogContent.getDialogObj(\"coas_dialog_" + this.dialogName + "\").Ok();'>" + this.cSubmitText + "</button>");
			if (otherButtons) {
				for (var n = 0; n < otherButtons.names.length; n++) {
					_coas_dialog_b.append("<button class='aui_state_highlight' id='"+ (otherButtons.ids[n]) +"' onclick='FDCDialogContent.getDialogObj(\"coas_dialog_" + this.dialogName + "\").cEditStatus=true;$(\"#coas_iframe_" + this.dialogName + "\")[0].contentWindow." + otherButtons.functions[n] + "(\"" + this.dialogName + "\");'>" + otherButtons.names[n] + "</button>");
				}
			}
			if (isCancelButton)
				_coas_dialog_b.append("<button class=''  onclick='FDCDialogContent.getDialogObj(\"coas_dialog_" + this.dialogName + "\").Close();'>" + this.cCancelText + "</button>");
	};
	/**��ı���**/
	this.reSetDialogTitle = function(titel) {
		var coas_dialog_m_h_l = $("#coas_dialog_m_h_l_" + this.dialogName);
		coas_dialog_m_h_l.html(titel);
	};

}
/** �����divģ̬������ **/
var FDCDialogContent = {
	curDialogName	: null,
	dialogNames	: new Array(),
	dialogObjs	: new Array(),
	setDialogObj : function(_dialogName, _dialogObj) {
		var i = this.dialogNames.length;
		this.dialogNames[i] = _dialogName;
		this.dialogObjs[i] = _dialogObj;
	},
	getDialogObj : function(_dialogName) {
		for (var i = 0; i < this.dialogNames.length; i++) {
			if (this.dialogNames[i] == _dialogName) return this.dialogObjs[i];
		}
	},
	removeDialogObj : function(_dialogName) {
		for (var i = 0; i < this.dialogNames.length; i++) {
			if (this.dialogNames[i] == _dialogName) {
				this.dialogObjs[i] = null;
				this.dialogNames[i] = null;
			}
		}
	}
};

/**
 * 
drowObj����Ҫ�϶���div id����div����
isScroll��div�Ƿ���ҳ�����ʱ����ҳ����λ����ʾ
 * @return
 */
var COASDrag = function(drawObj, isScroll) {
	if (typeof drawObj == "string")
		drawObj = document.getElementById(drawObj);  
	drawObj.orig_x = parseInt(drawObj.style.left) - document.body.scrollLeft;  
	drawObj.orig_y = parseInt(drawObj.style.top) - document.body.scrollTop;  
	drawObj.orig_index = drawObj.style.zIndex;  
	var id = "#"+drawObj.id;
	$(id).bind("mousedown", function(evt) {
		if(!evt) evt = window.event;
		var src = evt.srcElement ? evt.srcElement : evt.target;
		if(src && src.tagName && (src.tagName == 'INPUT' || src.tagName == 'SELECT' || src.tagName == 'TEXTAREA' || src.tagName == 'A')) return;
		var toMove = false;
		while(src && src.getAttribute) {
			if (src.getAttribute("moveDrag") != "true") {
				src = src.parentNode
			} else {
				toMove = true;
				break;
			}
		}
		if (!toMove) return;
		drawObj.style.cursor = "move";
        this.style.zIndex = 10000;
        
        var x = evt.clientX + document.body.scrollLeft - drawObj.offsetLeft;  
        var y = evt.clientY + document.body.scrollTop - drawObj.offsetTop;  
        $(document).bind("dragstart", function(evt) {return false;});
        $(document).bind("selectstart", function(evt) {return false;});
        $(document).bind("select", function(evt) {document.selection.empty();});
                  
        if(drawObj.setCapture)  
        	drawObj.setCapture();  
        else if(window.captureEvents)  
            window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
        
        $(document).bind("mousemove", function(evt) {  
            if(!evt) evt = window.event;  
            drawObj.style.left = evt.clientX + document.body.scrollLeft - x;
            var cy = evt.clientY + document.body.scrollTop - y;
            drawObj.style.top = cy <= 0 ? 0 : cy > (document.body.clientHeight - 30) ? (document.body.clientHeight - 30) : cy;  
            drawObj.orig_x = parseInt(drawObj.style.left) - document.body.scrollLeft;  
            drawObj.orig_y = parseInt(drawObj.style.top) - document.body.scrollTop;  
        });
        
        $(document).bind("mouseup", function() {
            if(drawObj.releaseCapture)
            	drawObj.releaseCapture();
            else if(window.captureEvents)
                window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
            $(document).unbind("mousemove");
            $(document).unbind("mouseup");
            $(document).unbind("dragstart");
            $(document).unbind("selectstart");
            $(document).unbind("select");
            drawObj.style.cursor = "";
            drawObj.style.zIndex = drawObj.orig_index;
        });
    });
      
    if (isScroll) {  
        var orig_scroll = window.onscroll ? window.onscroll : function (){};
        $(window).bind("scroll", function () {
        	if (drawObj.getAttribute("stopScroll") == 'true')
        		return;
            orig_scroll();
            drawObj.style.left = drawObj.orig_x + document.body.scrollLeft;
            drawObj.style.top = drawObj.orig_y + document.body.scrollTop;
        });
    }
};

